package com.example.registerdemo.service.impl;

import com.example.registerdemo.service.EmailService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.from}")
    private String from;

//    @RabbitListener(queues="directQueue")
    @Override
    public void sendHtmlMail(String to, String subject, String content) {
        // 负责监听消息队列信息,监听到信息,则发送一链接至对方邮箱,点击链接可以激活邮箱

        System.out.println("监听到消息："+to);

        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = null;
            helper = new MimeMessageHelper(message, true);
            //邮件发送人
            helper.setFrom(from);
            //邮件主题
            helper.setSubject(subject);
            //邮件接收人
            helper.setTo(to);
            //邮件内容
            helper.setText(content, true);
            //发送邮件
            mailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
